package guoanmaker.provider.business.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;

import guoanmaker.provider.business.model.OperatorServerClass;
import guoanmaker.provider.business.model.ProvideCommodity;
import guoanmaker.provider.business.model.ProvideRoom;
import guoanmaker.provider.business.modelnobind.ProvideCommodityMove;
import guoanmaker.provider.business.modelnobind.ProvideRoomMove;
import guoanmaker.provider.business.service.OperatorGuaranteeService;
import guoanmaker.provider.business.service.OperatorServerClassService;
import guoanmaker.provider.business.service.ProvideCommodityMoveServiceInterface;
import guoanmaker.provider.business.service.ProvideCommodityServiceInterface;
import guoanmaker.provider.helper.common.AjaxJson;
import guoanmaker.provider.helper.common.abstractobj.ApiController;
import guoanmaker.provider.helper.common.util.OSSUnitHelper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(ApiController.COMMODITYMOVE_URL)
public class ProvideCommodityMoveController {

	private final AjaxJson ajaxJson;
	
	@Autowired
    public ProvideCommodityMoveController(ProvideCommodityMoveServiceInterface commodityMoveService, AjaxJson ajaxJson) {
        this.commodityMoveService = commodityMoveService;
        this.ajaxJson = ajaxJson;
    }
	
	@Autowired
	private ProvideCommodityMoveServiceInterface commodityMoveService;
	
	@Autowired
	private OperatorGuaranteeService operatorGuaranteeService;
	
	@Autowired
	private OperatorServerClassService serverClassService;
	
	
	
	@ApiOperation(value="商品列表分页查询", notes="通过商品状态分页查询")
	@GetMapping(value = "/findCommdityMove")
	/***
	* @Title: getEntryCommdity 
	* @Description: 移动端通过类别查询商品列表
	* @param  page
	* @param  size
	* @param  service_type_id
	* @param  regionStart
	* @param  countrywideStart
	* @param @throws Exception    设定文件 
	* @return AjaxJson    返回类型  
	* @throws 
	 */
	public AjaxJson findCommdityMove( Integer page,
			 String service_type_id, String code, Integer regionStart
			, Integer countrywideStart,Integer reorderType )throws Exception  {
		try {
			int size=10;
			Map<String, Object> reMap=null;
			OperatorServerClass serverClass=serverClassService.getServerClassById(service_type_id);
			if(serverClass!=null){
				Integer type=serverClass.getServerType();//类别标记
				if(type==1){
					reMap=commodityMoveService.selectProvideCommodityRoomMoves(page, size, service_type_id, code,countrywideStart,regionStart, reorderType, serverClass);
				}else{
					reMap=commodityMoveService.selectProvideCommodityMoves(page, size, service_type_id,code,countrywideStart,regionStart,reorderType,serverClass);
				}
				ajaxJson.setSuccess(true).setStatus(200).setData(reMap);
			}else{
				ajaxJson.setSuccess(true).setStatus(250).setData(reMap);
			}
		} catch (Exception e) {
			return  ajaxJson.setSuccess(true).setStatus(250).setData(null);
		}
		return ajaxJson;

	}
	
	@ApiOperation(value="发布空间分页查询", notes="通过城市code分页查询")
	@GetMapping(value = "/findProvideRoomMoves")
	public AjaxJson findProvideRoomMoves( Integer page, String code)throws Exception  {
		try {
			List<ProvideRoomMove> rooms=(List<ProvideRoomMove>) commodityMoveService.selectProvideRoom(code,page,10);
			if(rooms.size()>0){
				ajaxJson.setSuccess(true).setStatus(200).setData(rooms);
			}else{
				ajaxJson.setSuccess(true).setStatus(250).setData(rooms);
			}
		} catch (Exception e) {
			return  ajaxJson.setSuccess(true).setStatus(250).setData(null);
		}
		return ajaxJson;

	}
	
	@ApiOperation(value="发布空间下的商品查询", notes="通过发布空间标识查询")
	@GetMapping(value = "/findProvideRoomCommodityMoves")
	public AjaxJson findProvideRoomCommodityMoves( String roomId)throws Exception  {
		Map<String, Object> reMap= commodityMoveService.selectByRoomId(roomId);
		ajaxJson.setSuccess(true).setStatus(200).setData(reMap);
		return ajaxJson;

	}
	
	 @ApiOperation(value="查询商品信息", notes="通过商品主键查询商品")
	 @GetMapping(value="getProvideCommodity")
	 public AjaxJson getProvideCommodityMove(String id,String cityCode,String userId,String secckillId)throws Exception  {
		 try {
			 synchronized (this) {
				 commodityMoveService.countCommditysClickRate(id);
			 }
			 ProvideCommodityMove commodity =commodityMoveService.selectById(id,cityCode,userId,secckillId);
			 if(commodity!=null){
				 String guarantee=operatorGuaranteeService.getGuarantee();
				 commodity.setGuarantee(guarantee);
				 ajaxJson.setSuccess(true).setStatus(200).setData(commodity);
			 }else{
				 return ajaxJson.setSuccess(true).setStatus(250).setData("没有查询到数据");
			 }
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxJson.setSuccess(true).setStatus(250).setData("没有查询到数据");
		}
			return ajaxJson;
	}
	 
	 @ApiOperation(value="统计商品的点击率", notes="统计商品的点击率")
	 @RequestMapping(value = "/countCommditysClickRate", method = RequestMethod.POST)
	 public AjaxJson countCommditysClickRate( String id){
		try {
			synchronized (this) {
				commodityMoveService.countCommditysClickRate(id);
			}
		} catch (Exception e) {
			return ajaxJson.setSuccess(true).setStatus(250).setData("数据操作失败！");
		}
		return ajaxJson.setSuccess(true).setStatus(200).setData(null);
	 }
	 
	
	/**
	 * 根据 关键字模糊查询与市级编号code精确查询空间与商品表
	 * @param code   市级code
	 * @param keyName   关键字
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value="根据 关键字模糊查询与市级编号code精确查询空间与商品表", notes="根据 关键字模糊查询与市级编号code精确查询空间与商品表")
	@GetMapping(value = "/selectKeyNameCodeSpaceCommodity")
	public AjaxJson selectKeyNameCodeSpaceCommodity(String code,String keyName)  {
		Map<String,Object> map=commodityMoveService.selectKeyNameCodeSpaceCommodity(code,keyName);
		ajaxJson.setSuccess(true).setStatus(200).setData(map);
		return ajaxJson;
	}
	 
	 @ApiOperation(value="通过商品code或空间code查询商品空间信息", notes="通过商品code或空间code查询商品空间信息")
	 @GetMapping(value="getProvideCommodityOrRoomMove")
	 public AjaxJson getProvideCommodityOrRoomMove(String code,String cityCode,String userId)throws Exception  {
		 try {
			 if(code.contains("S")){
				 Map<String, Object> reMap= commodityMoveService.selectByRoomCode(code);
				 ajaxJson.setSuccess(true).setStatus(200).setData(reMap);
			 }else{
				 synchronized (this) {
					 commodityMoveService.countCommditysClickRateByCode(code);
					 ProvideCommodityMove commodity =commodityMoveService.selectByCommodityCode(code,cityCode,userId);
					 String guarantee=operatorGuaranteeService.getGuarantee();
					 commodity.setGuarantee(guarantee);
					 ajaxJson.setSuccess(true).setStatus(200).setData(commodity);
				 }
			 }
		} catch (Exception e) {
			e.printStackTrace();
			return ajaxJson.setSuccess(true).setStatus(250).setData("没有查询到数据");
		}
			return ajaxJson;
	}
	
	 @ApiOperation(value="官网商品排行榜", notes="通过城市code查询商品排行榜")
	 @GetMapping(value = "/selectByCommoditys")
	 public AjaxJson selectByCommoditys(String cityCode,Integer size, Integer page, Integer type)throws Exception  {
		 try {
			 List<ProvideCommodityMove> commodityMoves= commodityMoveService.selectByCommoditys(cityCode,type,page,size);
			 if(commodityMoves.size()>0){
				 ajaxJson.setSuccess(true).setStatus(200).setData(commodityMoves);
			 }else{
				 ajaxJson.setSuccess(true).setStatus(250).setData(null);
			 }
		 } catch (Exception e) {
			 return  ajaxJson.setSuccess(true).setStatus(250).setData(null);
		 }
		 return ajaxJson;

	 }
}
